/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.Group;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerFieldManager;
/*     */ import com.newcrm.db.DealFieldManager;
/*     */ import com.newcrm.db.DealManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.ExcelUtil;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.Cookie;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class ExportDeals extends AbstractAction
/*     */ {
/*  41 */   private static Logger logger = Logger.getLogger(ExportDeals.class.getName());
/*     */ 
/*  43 */   private List<String[]> filters = new ArrayList();
/*     */ 
/*     */   public ExportDeals(AbstractHttpServlet servlet) {
/*  46 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  52 */     response.setContentType("application/force-download");
/*  53 */     response.addHeader("content-disposition", "filename=deals.xls");
/*     */ 
/*  55 */     Cookie[] cookies = request.getCookies();
/*  56 */     String filter = 
/*  57 */       Util.getCookieValue(cookies, "deals_filters");
/*     */ 
/*  59 */     Map parameters = Util.parseParameters(filter, this.filters);
/*     */ 
/*  61 */     int dealStatus = Util.getURLIntParameter((String)parameters.get("DealStatus"));
/*  62 */     int categoryId = Util.getURLIntParameter((String)parameters.get("CategoryId"));
/*  63 */     String responsibleUserId = Util.getURLParameter(
/*  64 */       (String)parameters
/*  64 */       .get("ResponsibleUserId"));
/*     */ 
/*  67 */     String createDate = Util.getURLParameter(request
/*  68 */       .getParameter("CreateDate"));
/*     */ 
/*  71 */     String wonDate = Util.getURLParameter(request.getParameter("WonDate"));
/*     */ 
/*  73 */     User loginUser = getLoginUser(request);
/*     */ 
/*  75 */     CompanyInfo company = super.getCompany(loginUser.companyId);
/*     */ 
/*  78 */     String groupIds = "";
/*  79 */     if ((!loginUser.isAdmin) && (!loginUser.isGroupAdmin)) {
/*  80 */       responsibleUserId = loginUser.userId;
/*  81 */     } else if (!loginUser.isAdmin) {
/*  82 */       Group group = company.getGroupById(loginUser.groupId);
/*  83 */       if (group != null)
/*  84 */         groupIds = group.getAllIds();
/*     */       else {
/*  86 */         groupIds = String.valueOf(loginUser.groupId);
/*     */       }
/*     */     }
/*     */ 
/*  90 */     DealManager manager = new DealManager();
/*  91 */     List result = new ArrayList();
/*  92 */     List fileds = null;
/*     */     try
/*     */     {
/*  95 */       result = manager.listAll(loginUser.isAdmin, dealStatus, categoryId, 
/*  96 */         responsibleUserId, groupIds, createDate, wonDate, this.filters, 
/*  97 */         loginUser.userId, 0, 0, loginUser.companyId);
/*     */ 
/*  99 */       List ids = new ArrayList();
/*     */ 
/* 101 */       Map dealIdIndex = new HashMap();
/*     */ 
/* 103 */       DealFieldManager dealFiledManager = new DealFieldManager();
/*     */       try {
/* 105 */         fileds = dealFiledManager.list(loginUser.companyId);
/*     */       } catch (DBException e) {
/* 107 */         logger.warn("db exception", e);
/*     */       } catch (SQLException e) {
/* 109 */         logger.warn("db exception", e);
/*     */       }
/*     */ 
/* 112 */       int i = 0;
/* 113 */        for (Object itemO : result) {
            Map item = (Map)itemO;
/* 114 */         String responsibleUserName = Util.getUserNameById(
/* 115 */           (String)item.get("ResponsibleUserId"), company.users);
/* 116 */         String createUserName = Util.getUserNameById(
/* 117 */           (String)item.get("CreateUserId"), company.users);
/* 118 */         String dealCategoryName = company.getDealCategoryName(
/* 119 */           Util.str2Int(item.get("CategoryId")));
/* 120 */         String dealStatusName = "";
/* 121 */         int status = Util.str2Int(item.get("DealStatus"));
/* 122 */         if (status == 3) {
/* 123 */           dealStatusName = "失败";
/* 124 */           item.put("DealSucessDate", "");
/* 125 */         } else if (status == 1) {
/* 126 */           dealStatusName = "跟踪";
/* 127 */           item.put("DealSucessDate", "");
/*     */         } else {
/* 129 */           dealStatusName = "成功";
/* 130 */           item.put("DealSucessDate", item.get("DealStatusChangeDate"));
/*     */         }
/*     */ 
/* 134 */         CustomerFieldManager fieldManager = new CustomerFieldManager();
/* 135 */         int customerId = ((Integer)item.get("CustomerId")).intValue();
/*     */ 
/* 137 */         List customerFields = fieldManager.list(
/* 138 */           loginUser.companyId, customerId, 
/* 139 */           2);
/* 140 */         StringBuffer email = new StringBuffer();
/* 141 */         int index = 0;
                for (Object emailsO : customerFields) {
                    Map emails = (Map)emailsO;
/* 143 */           if (index == 0)
/* 144 */             email.append(emails.get("Info1"));
/*     */           else {
/* 146 */             email.append(" ").append(emails.get("Info1"));
/*     */           }
/* 148 */           index++;
/*     */         }
/* 150 */         item.put("CustomerEmail", email);
/*     */ 
/* 152 */         item.put("ResponsibleUserName", responsibleUserName);
/* 153 */         item.put("CreateUserName", createUserName);
/* 154 */         item.put("DealCategoryName", dealCategoryName);
/* 155 */         item.put("DealStatusName", dealStatusName);
/*     */ 
/* 158 */         if ((fileds != null) && (fileds.size() > 0)) {
/* 159 */           dealIdIndex.put((Integer)item.get("DealId"), Integer.valueOf(i));
/* 160 */           ids.add((Integer)item.get("DealId"));
/* 161 */           if (ids.size() >= 10)
/*     */           {
/* 163 */             getDealExtInfo(ids, manager, result, dealIdIndex);
/*     */           }
/*     */         }
/* 166 */         i++;
/*     */       }
/* 168 */       if ((ids.size() > 0) && (fileds != null) && (fileds.size() > 0))
/* 169 */         getDealExtInfo(ids, manager, result, dealIdIndex);
/*     */     }
/*     */     catch (DBException e)
/*     */     {
/* 173 */       logger.warn("db exception", e);
/*     */     } catch (SQLException e) {
/* 175 */       logger.warn("SQLException", e);
/*     */     }
/*     */ 
/* 178 */     String[] subHeads = { "名称", "客户", "客户所属公司", "客户邮箱", "分类", "金额", "提成", 
/* 179 */       "负责人", "状态", "成交时间" };
/* 180 */     String[] subHeadsKey = { "Name", "CustomerName", "ParentName", 
/* 181 */       "CustomerEmail", "DealCategoryName", "Price", "Commission", 
/* 182 */       "ResponsibleUserName", "DealStatusName", "DealSucessDate" };
/*     */ 
/* 184 */     List heads = new ArrayList(Arrays.asList(subHeads));
/* 185 */     List headsKey = new ArrayList(
/* 186 */       Arrays.asList(subHeadsKey));
/*     */ 
/* 188 */     if ((fileds != null) && (fileds.size() > 0)) {
            for (Object fieldO : fileds) {
                Map field = (Map)fieldO;
/* 190 */         heads.add((String)field.get("DealFieldName"));
/* 191 */         headsKey.add("ExtInfo_" + field.get("DealFieldId"));
/*     */       }
/*     */     }
/* 194 */     heads.add("备注");
/* 195 */     heads.add("创建者");
/* 196 */     heads.add("创建时间");
/* 197 */     headsKey.add("Note");
/* 198 */     headsKey.add("CreateUserName");
/* 199 */     headsKey.add("CreateDate");
/* 200 */     ExcelUtil.export("销售机会", (String[])heads.toArray(new String[0]), 
/* 201 */       (String[])headsKey.toArray(new String[0]), result, null, 
/* 202 */       response.getOutputStream());
/*     */   }
/*     */ 
/*     */   private void getDealExtInfo(List<Integer> ids, DealManager manager, List<Map<String, Object>> result, Map<Integer, Integer> dealIdIndex)
/*     */   {
/* 215 */     StringBuffer dealIds = new StringBuffer();
/* 216 */     for (Integer id : ids) {
/* 217 */       dealIds.append(id).append(",");
/*     */     }
/*     */ 
/* 220 */     String tmp = dealIds.toString();
/* 221 */     tmp = tmp.substring(0, tmp.length() - 1);
/*     */     try
/*     */     {
/* 224 */       List extInfos = manager.getDealAllExtInfo(tmp);
/* 225 */       fillExtInfo(result, extInfos, dealIdIndex);
/*     */     } catch (Exception e) {
/* 227 */       logger.warn("get customers ext info failed", e);
/*     */     }
/* 229 */     ids.clear();
/*     */   }
/*     */ 
/*     */   private void fillExtInfo(List<Map<String, Object>> result, List<Map<String, Object>> extInfos, Map<Integer, Integer> dealIdIndex)
/*     */   {
/* 242 */     if ((extInfos == null) || (extInfos.size() == 0)) {
/* 243 */       return;
/*     */     }
/* 245 */     for (Map extInfo : extInfos)
/*     */     {
/* 247 */       int id = Util.str2Int(extInfo.get("DealId"));
/* 248 */       Integer idx = (Integer)dealIdIndex.get(Integer.valueOf(id));
/*     */ 
/* 250 */       if (idx != null)
/*     */       {
/* 254 */         if (idx.intValue() >= 0)
/*     */         {
/* 258 */           if (idx.intValue() <= result.size())
/*     */           {
/* 262 */             Map customer = (Map)result.get(idx.intValue());
/* 263 */             if (customer != null)
/*     */             {
/* 267 */               int type = Util.str2Int(extInfo.get("Type"));
/* 268 */               String value = (String)extInfo.get("Info1");
/* 269 */               customer.put("ExtInfo_" + type, value);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.ExportDeals
 * JD-Core Version:    0.6.1
 */